using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Globalization;

public partial class MasterPage : System.Web.UI.MasterPage
{
    static CultureInfo cul;
    static HttpCookie cookie=new HttpCookie("language");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadLang();
            TotalLanguage();
        }
    }
    public void loadLang()
    {
        languageBus langbus = new languageBus();
        try
        {

            if (cookie["language"] == null)
            {
                cookie["language"] = "2";
                Response.Cookies.Add(cookie);
            }
            else
            {
                cookie = Request.Cookies["language"];
            }
            if (Request.QueryString["lang_id"] != null)
            {
                cookie["language"] = Request.QueryString["lang_id"].ToString();
                cookie.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(cookie);

            }
            language lang = langbus.getLanguageByID(Convert.ToInt32(cookie["language"].ToString()));
            Page.UICulture = lang.Locale.ToString();
            Page.Culture = lang.Locale.ToString();
            cul = System.Threading.Thread.CurrentThread.CurrentCulture;

        }
        catch (Exception)
        {
            language lang = langbus.getLanguageByID(2);
            Page.Culture = lang.Locale.ToString();
            Page.UICulture = lang.Locale.ToString();
            cul = System.Threading.Thread.CurrentThread.CurrentCulture;
        }
    }
    public void TotalLanguage()
    {
        languageBus langbus = new languageBus();
        DataTable dt = langbus.getAllLanguage();
        DataTable listlanguage = new DataTable();
        listlanguage.Columns.Add("name");
        listlanguage.Columns.Add("id");
        listlanguage.Columns.Add("href");
        listlanguage.Columns.Add("image");
        DataRow dr;
        foreach (DataRow r in dt.Rows)
        {
            dr = listlanguage.NewRow();
            dr["name"] = r["name"].ToString();
            dr["id"] = r["language_id"];
            dr["image"] = "image/" + r["image"].ToString();
            dr["href"] = this.setLink("lang_id=" + r["language_id"].ToString());
            listlanguage.Rows.Add(dr);
        }
        listLang.DataSource = listlanguage;
        listLang.DataBind();

    }
    public string setLink(string link)
    {
        string path = "";
        if (Request.Path.Contains("?"))
        {
            path = Request.Path + "&" + link;
        }
        else
        {
            path = Request.Path + "?" + link;
        }
        return path;
    }
    public string getCountLanguage()
    {
        languageBus langbus = new languageBus();
        DataTable dt = langbus.getAllLanguage();
        return dt.Rows.Count.ToString();
    }

}
